﻿using ICSharpCode.SharpZipLib.Zip;
using PFMCandidate;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PFMHandlers
{
   public class PFMZipHandler:PFMHandler

    {
       public override byte[] Perform(Candidate aCandidate, byte[] baTarget)
       {
           byte[] zipResult = null;
           PFMFileHandler pFHandler = new PFMFileHandler();
           //baTarget = pFHandler.Perform(aCandidate, baTarget);
           baTarget = GetProcessingData(aCandidate, baTarget);
           using (MemoryStream ms = new MemoryStream())
           {
               using (ZipOutputStream zipStream = new
                   ZipOutputStream(ms))
               {
                   //将压缩包内的路径简单化
                   int a = aCandidate.Name.LastIndexOf(@"\")+1;
                   string b = aCandidate.Name.Substring(a, aCandidate.Name.Length - a);
                   ZipEntry entry = new ZipEntryFactory().
                       MakeFileEntry(b);
                   entry.DateTime = DateTime.Now;
                   entry.Size = baTarget.Length;
                   zipStream.PutNextEntry(entry);
                   zipStream.Write(baTarget, 0, baTarget.Length);
                   zipStream.CloseEntry();
                   zipStream.Finish();
                   zipResult = ms.ToArray();
                   zipStream.Close();
               }
           }
           pFHandler.Perform(aCandidate, zipResult);
           return baTarget;
           //return base.Peerform(aCandidate, baTarget);
       }
       //public string  UnZipFileToStream(PFMCandidate.Candidate aCandidate, byte[] baTarget)
       //{
       //    PFMFileHandler pFHandler = new PFMFileHandler();
       //    baTarget = pFHandler.Perform(aCandidate.Name, baTarget);
       //    Candidate bCandidate = new Candidate() { Name = @"E:\代码库\PFM作业\PFMHandlersTests\bin\新建文本文档.txt" };
       //    byte[] unzipResult = null;
       //    try
       //    {
       //        MemoryStream ms = new MemoryStream(baTarget);
       //        ZipEntry theEntry;
       //        using (ZipInputStream zipStream = new ZipInputStream(ms))
       //        {
       //            while ((theEntry = zipStream.GetNextEntry()) != null)
       //            {
       //                using (MemoryStream msDes = new MemoryStream())
       //                {
       //                    byte[] data = new byte[2048];
       //                    int size;
       //                    while (true)
       //                    {
       //                        size = zipStream.Read(data, 0, data.Length);
       //                        if (size > 0)
       //                        {
       //                            msDes.Write(data, 0, size);
       //                        }
       //                        else { break; }
       //                    }
       //                    unzipResult = msDes.ToArray();
       //                }
       //            }
       //            zipStream.Close();
       //        }
       //    }
       //    catch
       //    {
       //        unzipResult = null;
       //    }
       //    baTarget = pFHandler.Perform(bCandidate.Name, unzipResult);
       //    return bCandidate.Name;
       //}
      
    }
}
